<template>
  <div class="order">
    <div @click="toAddressList" v-if="address && address.userName" class="address">
      <div class="item">
        <div class="list">
          <div class="addresslist">
            <div>
              <span>{{address.userName}}</span>
              <div v-if="address.is_default" class="moren">
                默认
              </div>
            </div>
            <div class="info">
              <p>{{address.telNumber}}</p>
              <p>{{address.address+address.detailadress}}</p>
            </div>
            <div></div>
          </div>
        </div>
      </div>
    </div>
    <div @click="toAddressList" v-else class="seladdress">
      请选择收货地址
    </div>
    <div class="orderbox">
      <div class="item">
        <div>商品合计</div>
        <div>￥{{allprice}}</div>
      </div>
      <div class="item">
        <div>运费</div>
        <div>免运费</div>
      </div>
      <div class="item">
        <div>优惠券</div>
        <div>暂无</div>
      </div>
    </div>
    <div class="cartlist">
      <div class="item" v-for="(item,index) in listData" :key="index">
        <div class="con">
          <div class="left">
            <div class="img">
              <img :src="item.goods_id.imageList[0].url" alt="">
            </div>
            <div class="info">
              <p>{{item.goods_id.name}}</p>
              <p>￥{{item.goods_id.price}}</p>
            </div>
          </div>
          <div class="right">
            <div class="num">
              x{{item.amount}}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div>
        实付 : ￥{{allprice}}
      </div>
      <div @click="pay">
        支付
      </div>
    </div>
  </div>
</template>

<script>
  import AV from 'leancloud-storage'
  import {
    get,
    post,
    login,
    getStorageOpenid
  } from "../../utils";
  export default {
    onLoad(query){
      this.orderId = query.orderId;
      // console.log(query)
    },
    onShow() {
      // debugger;
       if (wx.getStorageSync("addressId")) {
        this.addressId = wx.getStorageSync("addressId");
      }
      this.openId = getStorageOpenid();

      this.getDetail();
    },
    created() {},
    mounted() {
     this.getDetail();
    },
    data() {
      return {
        addressId: "",
        openId: "",
        allprice: "",
        listData: [],
        address: {},
        orderId:'',
        // allAmount:0,
      };
    },
    components: {},
    methods: {
      pay() {
        wx.showLoading({
          title: '加载中' //数据请求前loading
        })
        let that = this;
        if(!this.addressId){
          wx.showToast({
            title:'请选择送货地址',
            icon:'warning',
            duration:3000,
            mask:false,
            success:res=>{
              that.toAddressList()
            }
          })
          wx.hideLoading();
          return;                    
        }

        // wx.showToast({
        //   title: "支付功能暂未开发", //提示的内容,
        //   icon: "none", //图标,
        //   duration: 1500, //延迟时间,
        //   mask: false, //显示透明蒙层，防止触摸穿透,
        //   success: res => {}
        // });
        console.log(this.allAmount);
        // debugger
        return AV.Cloud.run('order', {
          goods: this.listData,
          productDescription: '商品订单',
          price: this.allprice,
          // shopId:this.shopId,
          // openId:this.openId,
          addressId:this.addressId,
          orderId:this.orderId,
          goodsAmount:this.allAmount
        }).then(data=>{
          wx.hideLoading();
          console.log(data)
          console.log(1)
            // debugger
             return new Promise((resolve, reject) => {
            data.fail = function(err) {
              console.log('fail:',err);
              reject(err);
            };
            data.success = function(res) {
              console.log(res);              
              resolve(res);
            };
            wx.requestPayment(data)   
          });
         
        }).then(res=>{
          // 支付成功
          console.log(res)
          console.log(2)

          wx.showToast({
            title: "支付成功", //提示的内容,
            icon: "success", //图标,
            duration: 1500, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
            success: res => {
              console.log(3)
              // 
              wx.redirectTo({
                url:'/pages/orderlist/main'
              })
              // let　count = getCurrentPages();
              // // debugger;
              // if(count && count.length>0)
              // {
              //   console.log('count:',count.length);
              //   wx.navigateBack({
              //     delta: 2
              //   })
              // }
              // else
              //    wx.navigateBack({
              //     delta: 9
              //   })
              // 
              // wx.switchTab({
              //   url: '/pages/my/main'
              // })
            }
          });
        }).catch(err=>{
          
          wx.hideLoading();
          console.error(err)
          console.log(4)

          wx.switchTab({
            url: '/pages/cart/main'
          })          
        });
      },
      toAddressList() {
        wx.navigateTo({
          url: "/pages/addressSelect/main"
        });
      },
      toAdd() {
        wx.navigateTo({
          url: "/pages/addaddress/main"
        });
      },
      async getDetail() {
        
        const data = await get("/order/detailAction", {
          openId: this.openId,
          addressId: this.addressId,
          orderId:this.orderId
        });
        // console.log('获取到订单数据')
        // console.log(data);

        if (data) {
          this.allprice = data.allPrice;
          this.listData = data.goodsList;
          this.address = data.address;
          this.orderId = data.objectId;
        }
      }
    },
    computed: {
      allAmount(){
        let amount = 0;
        if(this.listData && this.listData.length){
          this.listData.forEach(element => {
            if(element.amount){
              amount +=element.amount;
            }
          });
        }
        return amount;
      }
    }
  };

</script>
<style lang='scss' scoped>
  @import "./style";
</style>
